var express = require('express');
var router = express.Router();
var multiparty = require("multiparty");
var path = require("path");
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.post("/upload", function (req, res) {
  var form = new multiparty.Form({ uploadDir: "./public/images" });
  form.parse(req, function (err, fields, files) {
    if (err) {
      res.send(err);
    } else {
      var p = files.file[0].path.substring(
        files.file[0].path.indexOf("images")
      );
      res.send({filename:path.basename(p)});
    }
  });
});

module.exports = router;
